using System.Collections.Generic;
using UnityEngine;

public class CatmullRomSpline : MonoBehaviour
{
	public Transform[] controlPointsList;

	public bool isLooping = true;

	public LineRenderer trail;

	private List<Vector3> nodes;

	private void OnDrawGizmos()
	{
		nodes = new List<Vector3>();
		Gizmos.color = Color.white;
		for (int i = 0; i < controlPointsList.Length; i++)
		{
			if ((i != 0 && i != controlPointsList.Length - 1 && i != controlPointsList.Length - 2) || isLooping)
			{
				DisplayCatmullRomSpline(i);
			}
		}
		trail.positionCount = nodes.Count;
		trail.SetPositions(nodes.ToArray());
	}

	private void DisplayCatmullRomSpline(int pos)
	{
		Vector3 position = controlPointsList[ClampListPos(pos - 1)].position;
		Vector3 position2 = controlPointsList[pos].position;
		Vector3 position3 = controlPointsList[ClampListPos(pos + 1)].position;
		Vector3 position4 = controlPointsList[ClampListPos(pos + 2)].position;
		Vector3 item = position2;
		nodes.Add(item);
		float num = 0.1f;
		int num2 = Mathf.FloorToInt(1f / num);
		for (int i = 1; i <= num2; i++)
		{
			float t = (float)i * num;
			Vector3 catmullRomPosition = GetCatmullRomPosition(t, position, position2, position3, position4);
			nodes.Add(catmullRomPosition);
			item = catmullRomPosition;
		}
	}

	private int ClampListPos(int pos)
	{
		if (pos < 0)
		{
			pos = controlPointsList.Length - 1;
		}
		if (pos > controlPointsList.Length)
		{
			pos = 1;
		}
		else if (pos > controlPointsList.Length - 1)
		{
			pos = 0;
		}
		return pos;
	}

	private Vector3 GetCatmullRomPosition(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
	{
		Vector3 vector = 2f * p1;
		Vector3 vector2 = p2 - p0;
		Vector3 vector3 = 2f * p0 - 5f * p1 + 4f * p2 - p3;
		Vector3 vector4 = -p0 + 3f * p1 - 3f * p2 + p3;
		return 0.5f * (vector + vector2 * t + vector3 * t * t + vector4 * t * t * t);
	}
}
